home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 3 / Info_Mac_1994-01.iso / Development / Source / MultiSession 1.04 Source / Core 27⁄June⁄1993 / Audit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-03  |  9.1 KB  |  398 lines  |  [TEXT/KAHL]

  1. /* Audit.c */
  2.  
  3. #include "Audit.h"
  4. #include "MiscInfo.h"
  5.  
  6.  
  7. #ifdef AUDIT
  8.  
  9. typedef void *va_list;
  10. #define __va(arg)                &arg + 1
  11. #define va_start(p, arg)        p = __va(arg)
  12. #define va_arg(p, type)            *(* (type **) &p)++
  13. #define va_end(p)
  14.  
  15. static short    AuditRefNum;
  16.  
  17. #if __option(mc68020)
  18.     #define CodeFor68020
  19. #else
  20.     #define CodeFor68000
  21. #endif
  22. #pragma options(!mc68020) /* this code works no matter what */
  23.  
  24.  
  25. void        INITAUDIT(void)
  26.     {
  27.         char        FileName[] = {"\p Audit Trail"};
  28.  
  29.         FSDelete((unsigned char*)FileName,0);
  30.         ERROR(Create((unsigned char*)FileName,0,AUDITCREATOR,'TEXT') != noErr,
  31.             PRERR(ForceAbort,"Audit_Init couldn't create audit trail file."));
  32.         ERROR(FSOpen((unsigned char*)FileName,0,&AuditRefNum) != noErr,
  33.             PRERR(ForceAbort,"Audit_Init couldn't open audit trail file for writing."));
  34.     }
  35.  
  36. void        ENDAUDIT(void)
  37.     {
  38.         FSClose(AuditRefNum);
  39.     }
  40.  
  41. static void    FlushBuffer(char Buffer[1024], short* BufPtr, MyBoolean FlushAllFlag)
  42.     {
  43.         char*        Place;
  44.         long        Count;
  45.  
  46.         if ((*BufPtr >= 512) || (FlushAllFlag))
  47.             {
  48.                 Place = Buffer;
  49.                 while (*BufPtr != 0)
  50.                     {
  51.                         Count = *BufPtr;
  52.                         FSWrite(AuditRefNum,&Count,Place);
  53.                         *BufPtr -= Count;
  54.                         Place += Count;
  55.                     }
  56.             }
  57.     }
  58.  
  59.  
  60. void        AHEXDUMP(char* Ptr, long NumBytes)
  61.     {
  62.         while (NumBytes >= 8)
  63.             {
  64.                 AuditPrint("%xc %xc %xc %xc %xc %xc %xc %xc",
  65.                     (uchar)*Ptr,(uchar)*(Ptr+1),(uchar)*(Ptr+2),(uchar)*(Ptr+3),
  66.                     (uchar)*(Ptr+4),(uchar)*(Ptr+5),(uchar)*(Ptr+6),(uchar)*(Ptr+7));
  67.                 NumBytes -= 8;
  68.                 Ptr += 8;
  69.             }
  70.         switch (NumBytes)
  71.             {
  72.                 case 1:
  73.                     AuditPrint("%xc",(uchar)*Ptr);
  74.                     break;
  75.                 case 2:
  76.                     AuditPrint("%xc %xc",(uchar)*Ptr,(uchar)*(Ptr+1));
  77.                     break;
  78.                 case 3:
  79.                     AuditPrint("%xc %xc %xc",
  80.                         (uchar)*Ptr,(uchar)*(Ptr+1),(uchar)*(Ptr+2));
  81.                     break;
  82.                 case 4:
  83.                     AuditPrint("%xc %xc %xc %xc",
  84.                         (uchar)*Ptr,(uchar)*(Ptr+1),(uchar)*(Ptr+2),(uchar)*(Ptr+3));
  85.                     break;
  86.                 case 5:
  87.                     AuditPrint("%xc %xc %xc %xc %xc",
  88.                         (uchar)*Ptr,(uchar)*(Ptr+1),(uchar)*(Ptr+2),(uchar)*(Ptr+3),
  89.                         (uchar)*(Ptr+4));
  90.                     break;
  91.                 case 6:
  92.                     AuditPrint("%xc %xc %xc %xc %xc %xc",
  93.                         (uchar)*Ptr,(uchar)*(Ptr+1),(uchar)*(Ptr+2),(uchar)*(Ptr+3),
  94.                         (uchar)*(Ptr+4),(uchar)*(Ptr+5));
  95.                     break;
  96.                 case 7:
  97.                     AuditPrint("%xc %xc %xc %xc %xc %xc %xc",
  98.                         (uchar)*Ptr,(uchar)*(Ptr+1),(uchar)*(Ptr+2),(uchar)*(Ptr+3),
  99.                         (uchar)*(Ptr+4),(uchar)*(Ptr+5),(uchar)*(Ptr+6));
  100.                     break;
  101.             }
  102.     }
  103.  
  104.  
  105. /* this prints a string in the same way that printf does.  it accepts these options: */
  106. /* %s = decimal signed short */
  107. /* $xs = hexadecimal short */
  108. /* %l = decimal signed long */
  109. /* %xl = hexadecimal long */
  110. /* %b = boolean from short */
  111. /* %c = decimal signed char */
  112. /* %xc = hexadecimal char */
  113. /* %t = C String (text) */
  114. /* %p = Pascal string */
  115. void        AuditPrint(char* Str,...)
  116.     {
  117.         va_list            pa;
  118.         char                Buffer[1024];
  119.         short                BufPtr;
  120.         static char    Hex[16] = {'0','1','2','3','4','5','6','7',
  121.                                     '8','9','a','b','c','d','e','f'};
  122.         short                VRefNum;
  123.  
  124.         BufPtr = 0;
  125.         va_start(pa,Str);
  126.         while (*Str != 0)
  127.             {
  128.                 if (*Str == '%')
  129.                     {
  130.                         MyBoolean        HexFlag;
  131.  
  132.                         Str += 1;
  133.                         if (HexFlag = (*Str == 'x'))
  134.                             {
  135.                                 Str += 1;
  136.                             }
  137.                         switch (*Str)
  138.                             {
  139.                                 case 'l':
  140.                                     {
  141.                                         long    Num;
  142.  
  143.                                         Num = va_arg(pa,long);
  144.                                         if (HexFlag)    
  145.                                             {
  146.                                                 char        Buf[9];
  147.                                                 short        Count;
  148.                                         
  149.                                                 for (Count = 8; Count >= 1; Count -= 1)
  150.                                                     {
  151.                                                         Buf[Count] = Hex[Num & 0x0000000f];
  152.                                                         Num = Num >> 4;
  153.                                                     }
  154.                                                 Buf[0] = '$';
  155.                                                 for (Count = 0; Count < 9; Count += 1)
  156.                                                     {
  157.                                                         Buffer[BufPtr++] = Buf[Count];
  158.                                                     }
  159.                                             }
  160.                                          else
  161.                                             {
  162.                                                 char        Buf[16];
  163.                                                 short        BPtr;
  164.                                                 short        Scan;
  165.  
  166.                                                 if (Num == -2147483648)
  167.                                                     {
  168.                                                         char        Apl[] = "-2147483648";
  169.                                                         short        Scan;
  170.  
  171.                                                         for (Scan = 0; Apl[Scan] != 0; Scan += 1)
  172.                                                             {
  173.                                                                 Buffer[BufPtr++] = Apl[Scan];
  174.                                                             }
  175.                                                     }
  176.                                                  else
  177.                                                     {
  178.                                                         if (Num < 0)
  179.                                                             {
  180.                                                                 Buffer[BufPtr++] = '-';
  181.                                                                 Num = -Num;
  182.                                                             }
  183.                                                         BPtr = 16;
  184.                                                         Buf[--BPtr] = 0;
  185.                                                         do
  186.                                                             {
  187.                                                                 Buf[--BPtr] = (Num % 10) + '0';
  188.                                                                 Num = Num / 10;
  189.                                                             } while (Num != 0);
  190.                                                         for (Scan = BPtr; Scan < 16; Scan += 1)
  191.                                                             {
  192.                                                                 Buffer[BufPtr++] = Buf[Scan];
  193.                                                             }
  194.                                                     }
  195.                                             }
  196.                                     }
  197.                                     Str += 1;
  198.                                     break;
  199.                                 case 's':
  200.                                     {
  201.                                         short Num;
  202.  
  203.                                         Num = va_arg(pa,short);
  204.                                         if (HexFlag)
  205.                                             {
  206.                                                 char        Buf[5];
  207.                                                 short        Count;
  208.  
  209.                                                 for (Count = 4; Count >= 1; Count -= 1)
  210.                                                     {
  211.                                                         Buf[Count] = Hex[Num & 0x000f];
  212.                                                         Num = Num >> 4;
  213.                                                     }
  214.                                                 Buf[0] = '$';
  215.                                                 for (Count = 0; Count < 5; Count += 1)
  216.                                                     {
  217.                                                         Buffer[BufPtr++] = Buf[Count];
  218.                                                     }
  219.                                             }
  220.                                          else
  221.                                             {
  222.                                                 if (Num == -32768)
  223.                                                     {
  224.                                                         char        Apl[] = "-32768";
  225.                                                         short        Scan;
  226.     
  227.                                                         for (Scan = 0; Apl[Scan] != 0; Scan += 1)
  228.                                                             {
  229.                                                                 Buffer[BufPtr++] = Apl[Scan];
  230.                                                             }
  231.                                                     }
  232.                                                  else
  233.                                                     {
  234.                                                         char        Buf[8];
  235.                                                         short        BPtr;
  236.                                                         short        Scan;
  237.     
  238.                                                         if (Num < 0)
  239.                                                             {
  240.                                                                 Buffer[BufPtr++] = '-';
  241.                                                                 Num = -Num;
  242.                                                             }
  243.                                                         BPtr = 8;
  244.                                                         Buf[--BPtr] = 0;
  245.                                                         do
  246.                                                             {
  247.                                                                 Buf[--BPtr] = (Num % 10) + '0';
  248.                                                                 Num = Num / 10;
  249.                                                             } while (Num != 0);
  250.                                                         for (Scan = BPtr; Scan < 8; Scan += 1)
  251.                                                             {
  252.                                                                 Buffer[BufPtr++] = Buf[Scan];
  253.                                                             }
  254.                                                     }
  255.                                             }
  256.                                     }
  257.                                     Str += 1;
  258.                                     break;
  259.                                 case 'c':
  260.                                     {
  261.                                         char     Num;
  262.  
  263.                                         Num = ((va_arg(pa,short)) >> 0) & 0x00ff;
  264.                                         if (HexFlag)
  265.                                             {
  266.                                                 char        Buf[3];
  267.                                                 short        Count;
  268.  
  269.                                                 for (Count = 2; Count >= 1; Count -= 1)
  270.                                                     {
  271.                                                         Buf[Count] = Hex[Num & 0x0f];
  272.                                                         Num = Num >> 4;
  273.                                                     }
  274.                                                 Buf[0] = '$';
  275.                                                 for (Count = 0; Count < 3; Count += 1)
  276.                                                     {
  277.                                                         Buffer[BufPtr++] = Buf[Count];
  278.                                                     }
  279.                                             }
  280.                                          else
  281.                                             {
  282.                                                 if (Num == -128)
  283.                                                     {
  284.                                                         char        Apl[] = "-128";
  285.                                                         short        Scan;
  286.     
  287.                                                         for (Scan = 0; Apl[Scan] != 0; Scan += 1)
  288.                                                             {
  289.                                                                 Buffer[BufPtr++] = Apl[Scan];
  290.                                                             }
  291.                                                     }
  292.                                                  else
  293.                                                     {
  294.                                                         char        Buf[4];
  295.                                                         short        BPtr;
  296.                                                         short        Scan;
  297.     
  298.                                                         if (Num < 0)
  299.                                                             {
  300.                                                                 Buffer[BufPtr++] = '-';
  301.                                                                 Num = -Num;
  302.                                                             }
  303.                                                         BPtr = 4;
  304.                                                         Buf[--BPtr] = 0;
  305.                                                         do
  306.                                                             {
  307.                                                                 Buf[--BPtr] = (Num % 10) + '0';
  308.                                                                 Num = Num / 10;
  309.                                                             } while (Num != 0);
  310.                                                         for (Scan = BPtr; Scan < 4; Scan += 1)
  311.                                                             {
  312.                                                                 Buffer[BufPtr++] = Buf[Scan];
  313.                                                             }
  314.                                                     }
  315.                                             }
  316.                                             Str += 1;
  317.                                             break;
  318.                                         case 't':
  319.                                             {
  320.                                                 char*        Strp;
  321.  
  322.                                                 Strp = va_arg(pa,char*);
  323.                                                 while (*Strp != 0)
  324.                                                     {
  325.                                                         Buffer[BufPtr++] = *(Strp++);
  326.                                                         FlushBuffer(Buffer,&BufPtr,False);
  327.                                                     }
  328.                                             }
  329.                                             Str += 1;
  330.                                             break;
  331.                                         case 'p':
  332.                                             {
  333.                                                 char*            Temp;
  334.                                                 short            Scan;
  335.  
  336.                                                 Temp = va_arg(pa,char*);
  337.                                                 for (Scan = 0; Scan != Temp[0]; Scan += 1)
  338.                                                     {
  339.                                                         Buffer[BufPtr++] = Temp[Scan+1];
  340.                                                     }
  341.                                             }
  342.                                             Str += 1;
  343.                                             break;
  344.                                         case 'b':
  345.                                             {
  346.                                                 MyBoolean    Num;
  347.                                                 char*            Strptr;
  348.                                                 short            Cnt;
  349.  
  350.                                                 Num = va_arg(pa,short);
  351.                                                 if (Num)
  352.                                                     {
  353.                                                         Strptr = "True";
  354.                                                     }
  355.                                                  else
  356.                                                     {
  357.                                                         Strptr = "False";
  358.                                                     }
  359.                                                 for (Cnt = 0; Strptr[Cnt] != 0; Cnt += 1)
  360.                                                     {
  361.                                                         Buffer[BufPtr++] = Strptr[Cnt];
  362.                                                     }
  363.                                             }
  364.                                             Str += 1;
  365.                                             break;
  366.                                         default:
  367.                                             {
  368.                                                 short        Cnt;
  369.                                                 char        Msg[] = "???";
  370.  
  371.                                                 for (Cnt = 0; Msg[Cnt] != 0; Cnt += 1)
  372.                                                     {
  373.                                                         Buffer[BufPtr++] = Msg[Cnt];
  374.                                                     }
  375.                                             }
  376.                                             Str += 1;
  377.                                             break;
  378.                                     }
  379.                             }
  380.                     }
  381.                  else
  382.                     {
  383.                         Buffer[BufPtr++] = *(Str++);
  384.                     }
  385.                 FlushBuffer(Buffer,&BufPtr,False);
  386.             }
  387.         Buffer[BufPtr++] = 0x0d;
  388.         FlushBuffer(Buffer,&BufPtr,True);
  389.         GetVRefNum(AuditRefNum,&VRefNum);
  390. //        FlushVol("\p",VRefNum);
  391.     }
  392.  
  393. #ifdef CodeFor68020
  394.     #pragma options(mc68020) /* turn it back on if necessary */
  395. #endif
  396.  
  397. #endif
  398.